import ampub from 'ampub';
import constants from '../../constants';


const { utils: { multiLangUtils: { getMultiLangByID }, msgUtils: { showMessage } } } = ampub;
const { UISTATE, AREA: { CARD: { BASIC_INFO_AREA: basicInfoForm } } } = constants;
/**
 * 使用权资产的编辑前事件处理
 * props, moduleId(区域id), key(操作的键), value（当前值）,data(当前表单所有值)
 */

export default function usufructAssetBeforeEvent(props, moduleId, key, value, data) {
	const { form } = props;
    const status = props.getUrlParam('status');

	// 资产类别必须优先填入
	let pk_category = form.getFormItemsValue(basicInfoForm, 'pk_category').value;
	if (!pk_category) {
		/* 国际化：请先输入资产类别 */
		showMessage(props, { content: getMultiLangByID('201201504A-000098'), color: 'warning' });
		// 自动聚焦“资产类别”
		form.setFormItemFocus(basicInfoForm, 'pk_category');
		return false;
    }

	// 资产租入合同，禁止编辑使用权页签
	if (this.bill_source_4A3A) {
		return false;
    }
    
    /* 租金标准 */
	if (key == 'rent_standard') {
		let step_rent = form.getFormItemsValue(basicInfoForm, 'step_rent').value;
        return this.isAllowChangeHeadWhenEdit() && !step_rent;
	}

    /* 续租截止日期 */
	if (key == 'relet_ending_date') {
		let renewal_flag = form.getFormItemsValue(moduleId, 'renewal_flag').value;
		if (!renewal_flag) {
			return false;
		}
    }

    /* 购买价格 */
	if (key == 'purchase_price') {
		let purchase_choice = form.getFormItemsValue(moduleId, 'purchase_choice').value;
		if (!purchase_choice) {
			return false;
		}
	}

	if(status === UISTATE.EDIT){
        // 可修订权限
        let isRevise = this.reviseFields.includes(key);

        // 没有后续操作直接允许修改，否则看是否可修订
        let allow = this.isAllowChangeHeadWhenEdit() || isRevise;
        if(!allow){
            return false;
        }
	}
    
	return true;
}
